#import "sprite_game_enemy_boss1.h"

@implementation SpriteGameEnemyBoss1

- (id)initSpriteGameEnemyBoss1
{
	if ((self = [super initSpriteGameEnemy]))
	{
		objectName_ = "object sprite game enemy type boss 1";
		
		/*box2d
		*/
		fixtureHead_ = NULL;
		fixtureBody_ = NULL;
	}
	return self;
}

- (void)tickBoss1StrongAttack:(ccTime)Time
{
	[self ActionBoss1StrangAttack];
}

- (void)createBody
{
	int i;
	int _VertexCount;
	float _Ratio;
	b2Vec2 *_Vertex;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;

	[super createBody];

	_Ratio = 1 / CMgrGamePhysics::Ratio();
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;
	
	/*head
	*/
	_VertexCount = g_CfgGameBoss1Data.box2d_collision_point_table_head.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss1Data.box2d_collision_point_table_head);
	for (i = 0; i < _VertexCount; i++)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	_PolygonShape.Set(_Vertex, _VertexCount);
	free(_Vertex);
	_FixtureDef.shape = &_PolygonShape;
	fixtureHead_ = box2dBody_->CreateFixture(&_FixtureDef);
	/*body
	*/
	_VertexCount = g_CfgGameBoss1Data.box2d_collision_point_table_body.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss1Data.box2d_collision_point_table_body);
	for (i = 0; i < _VertexCount; i++)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	_PolygonShape.Set(_Vertex, _VertexCount);
	free(_Vertex);
	_FixtureDef.shape = &_PolygonShape;
	fixtureBody_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)destroyBody
{
	[super destroyBody];
	fixtureHead_ = NULL;
	fixtureBody_ = NULL;
}

- (void)ActionBoss1Appear
{
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBackAppearMoveOver;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS1_APPEAR;
	[self PlayAni:ENEMY_ANI_MOVE];
	_ActionMove = tcCreateActionMove(self.positionInPixels, g_CfgGameScrrenArea[4].center, [self GetMoveSpeed]);
	_CallBackAppearMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionAppearOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBackAppearMoveOver, nil];
	[self runAction:_Seq];
}

- (void)ActionBoss1StageChage
{
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBackMoveOver;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS1_STAGE_CHANGE;
	[self StopAction];
	[self unschedule:@selector(tickBoss1StrongAttack:)];
	[g_LayerGameObject Boss1StrongAttackCancel:&boss1StrongAttackBombTable_];
	switch (flagBoss1Stage_)
	{
	case BOSS_1_STAGE_1: flagBoss1Stage_ = BOSS_1_STAGE_2; break;
	case BOSS_1_STAGE_2: flagBoss1Stage_ = BOSS_1_STAGE_3; break;
	case BOSS_1_STAGE_3: assert(false); break;
	default:;
	}
	[self PlayAni:ENEMY_ANI_MOVE];
	_ActionMove = tcCreateActionMove(self.positionInPixels, g_CfgGameScrrenArea[4].center, [self GetMoveSpeed]);
	_CallBackMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionStageChangeMoveOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBackMoveOver, nil];
	[self runAction:_Seq];
}

- (void)ActionBoss1StrangAttack
{
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBack;

	if (state_ == ENEMY_STATE_DIE)
	{
		return;
	}

	state_ = ENEMY_STATE_BOSS1_STRONG_ATTACK_MOVE_TO_START;
	[self StopAction];
	[self unschedule:@selector(tickBoss1StrongAttack:)];
	[self PlayAni:ENEMY_ANI_MOVE];
	_ActionMove = tcCreateActionMove(self.positionInPixels, g_CfgGameScrrenArea[6].center, [self GetMoveSpeed]);
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionStrongAttackMoveStartPositionOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBack, nil];
	[self runAction:_Seq];
}

- (void)CallBackBoss1ActionAppearOver:(id)Sender
{
	[self schedule:@selector(tickBoss1StrongAttack:) interval:15.0f];
	[self AiActionStandOver];
}

- (void)CallBackBoss1ActionStageChangeMoveOver:(id)Sender
{
	switch (flagBoss1Stage_)
	{
	case BOSS_1_STAGE_1: assert(false); break;
	case BOSS_1_STAGE_2:
		[tailLeft_ ActionAppear];
		[self runAction:tcCreateActionCallBack(self, 1.0f, @selector(CallBackBoss1ActionStageChangeOver:))];
		break;
	case BOSS_1_STAGE_3:
		[tailLeft_ ActionAppear];
		[tailRight_ ActionAppear];
		[self runAction:tcCreateActionCallBack(self, 1.0f, @selector(CallBackBoss1ActionStageChangeOver:))];
		break;
	default:;
	}
}

- (void)CallBackBoss1ActionStageChangeOver:(id)Sender
{
	[self schedule:@selector(tickBoss1StrongAttack:) interval:15.0f];
	[self AiActionMoveOver];

	/*music
	*/
	switch (flagBoss1Stage_)
	{
	case BOSS_1_STAGE_2: MusicPlayGameBossStage2(); break;
	case BOSS_1_STAGE_3: MusicPlayGameBossStage3(); break;
	}
}

- (void)CallBackBoss1ActionStrongAttackMoveStartPositionOver:(id)Sender
{
	float _Distance;
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBack;
	sprite_game_bomb *_Bomb;

	_Distance = g_CfgGameScrrenArea[8].center.x - g_CfgGameScrrenArea[6].center.x;
	switch (flagBoss1Stage_)
	{
	case BOSS_1_STAGE_1: flagBoss1StrongAttackTotalDropBombCount_ = 5; break;
	case BOSS_1_STAGE_2: flagBoss1StrongAttackTotalDropBombCount_ = 8; break;
	case BOSS_1_STAGE_3: flagBoss1StrongAttackTotalDropBombCount_ = 10; break;
	default:;
	}
	flagBoss1StrongAttackDropBombCount_ = 0;
	flagBoss1StrongAttackDropBombOffset_ = _Distance / (float)flagBoss1StrongAttackTotalDropBombCount_;
	_ActionMove = tcCreateActionMove(self.positionInPixels, ccp(self.positionInPixels.x + flagBoss1StrongAttackDropBombOffset_, self.positionInPixels.y), [self GetMoveSpeed]);
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionStrongAttackMovePlaceBombPositionOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBack, nil];
	[self runAction:_Seq];

	/*create bomb
	*/
	_Bomb = [[sprite_game_bomb alloc] initSpriteGameBomb:g_CfgGameBoss1Data.bomb_type];
	[g_LayerGameObject AddBomb:_Bomb];
	_Bomb.positionInPixels = self.positionInPixels;
	boss1StrongAttackBombTable_.push_back(_Bomb);
	flagBoss1StrongAttackDropBombCount_++;
}

- (void)CallBackBoss1ActionStrongAttackMovePlaceBombPositionOver:(id)Sender
{
	CCSequence *_Seq;
	id _ActionMove;
	CCCallFuncN *_CallBackMoveOver;
	id _ActionAni;
	CCCallFuncN *_CallBackAniOver;
	sprite_game_bomb *_Bomb;

	if (flagBoss1StrongAttackDropBombCount_ < flagBoss1StrongAttackTotalDropBombCount_)
	{
		/*action move
		*/
		_ActionMove = tcCreateActionMove(self.positionInPixels, ccp(self.positionInPixels.x + flagBoss1StrongAttackDropBombOffset_, self.positionInPixels.y), [self GetMoveSpeed]);
		_CallBackMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionStrongAttackMovePlaceBombPositionOver:)];
		_Seq = [CCSequence actions:_ActionMove, _CallBackMoveOver, nil];
		[self runAction:_Seq];

		/*create bomb
		*/
		_Bomb = [[sprite_game_bomb alloc] initSpriteGameBomb:g_CfgGameBoss1Data.bomb_type];
		[g_LayerGameObject AddBomb:_Bomb];
		_Bomb.positionInPixels = self.positionInPixels;
		boss1StrongAttackBombTable_.push_back(_Bomb);
		flagBoss1StrongAttackDropBombCount_++;
	}
	else
	{
		[self StopAction];
		_ActionAni = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgGameBoss1Data.ani_attack)];
		_CallBackAniOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBoss1ActionStrongAttackAniOver:)];
		_Seq = [CCSequence actions:_ActionAni, _CallBackAniOver, nil];
		[image_ runAction:_Seq];
	}
}

- (void)CallBackBoss1ActionStrongAttackAniOver:(id)Sender
{
	[g_LayerGameObject Boss1StrongAttackBombMove:&boss1StrongAttackBombTable_];
	boss1StrongAttackBombTable_.clear();
	[self schedule:@selector(tickBoss1StrongAttack:) interval:15.0f];
	[self AiActionAttackOver];
}

@end